package com.leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * 3. 无重复字符的最长子串
 * 滑动窗口 + hash
 */
public class Solution3_1 {
    public int lengthOfLongestSubstring(String s) {
        Set<Character> charSet = new HashSet<>();
        int n = s.length();
        int l = 0;
        int r = -1;
        int length = 0;
        while (l < n) {
            while (r + 1 < n && !charSet.contains(s.charAt(r + 1))) {
                charSet.add(s.charAt(++r));
            }
            length = Math.max(length, r - l + 1);
            charSet.remove(s.charAt(l++));
        }
        return length;
    }

    public static void main(String[] args) {
        String s = "abcabcbb";
        int res = new Solution3_1().lengthOfLongestSubstring(s);
        System.out.println(res);
    }
}
